package com.rc.saas.tenant.service.acct;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.component.UidGenerator;
import com.rc.saas.tenant.mapper.acct.AcctBillOrderMapper;
import com.rc.saas.tenant.mapper.acct.AcctRuleMapper;
import com.rc.saas.tenant.model.acct.*;
import com.rc.saas.tenant.service.order.OrderInfoService;
import com.rc.saas.tenant.vo.acct.*;
import com.rc.saas.tenant.search.acct.AcctBillOrderSearch;
import com.rc.saas.tenant.search.acct.OrderAcctBillOrderSearch;
import com.rc.saas.tenant.vo.order.AcctRuleOrderResult;
import com.rc.saas.tenant.vo.order.OrderPrintVo;
import com.rc.saas.tenant.vo.order.OrderResult;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author frog
 * @date 2021/1/13
 */
@Service
public class AcctBillOrderServiceImpl implements AcctBillOrderService {
    private static Logger logger = LogManager.getLogger(AcctBillOrderServiceImpl.class);

    @Autowired
    private AcctBillOrderMapper acctBillOrderMapper;

    @Autowired
    private TenantOperatorAccountService tenantOperatorAccountService;

    @Autowired
    private TenantOperatorAccountRecordService tenantOperatorAccountRecordService;

    @Autowired
    private AcctRuleMapper acctRuleMapper;

    @Autowired
    @Lazy
    private OrderInfoService orderInfoService;

    @Autowired
    private UidGenerator uidGenerator;

    @Override
    public AcctBillOrderMapper getMapper() {
        return acctBillOrderMapper;
    }

    @Override
    public PageInfo<AcctBillOrderResultVo> listPageAcctBillOrder(AcctBillOrderSearch acctBillOrderSearch) {
        PageHelper.startPage(acctBillOrderSearch.getOffset(), acctBillOrderSearch.getLimit());
        List<AcctBillOrderResultVo> list = acctBillOrderMapper.listPageAcctBillOrder(acctBillOrderSearch);
        return new PageInfo<AcctBillOrderResultVo>(list);
    }

    @Override
    public AcctBillOrderResultVo selectAcctBillOrder(Long billOrderId) {
        return acctBillOrderMapper.selectAcctBillOrder(billOrderId);
    }

    @Override
    public PageInfo<OrderResult> listPageByCondition(OrderAcctBillOrderSearch orderSearch) {
        PageHelper.startPage(orderSearch.getOffset(), orderSearch.getLimit());
        List<OrderResult> orderList = acctBillOrderMapper.listByCondition(orderSearch);
        return new PageInfo<OrderResult>(orderList);
    }

    @Override
    public BigDecimal sumUnconfirmedAmount(String tenantOperatorCode) {
        return acctBillOrderMapper.sumUnconfirmedAmount(tenantOperatorCode);
    }

    @Override
    public TenantOperatorOrderDataVo sumTenantOperatorOrderData(Long stationId, String startTime, String endTime, String tenantOperatorCode) {
        return acctBillOrderMapper.sumTenantOperatorOrderData(stationId, startTime, endTime, tenantOperatorCode);
    }

    @Override
    public List<TenantOperatorOrderDataECharVo> sumTenantOperatorOrderECharData(Long stationId, String startTime, String endTime, String tenantOperatorCode) {
        return acctBillOrderMapper.sumTenantOperatorOrderECharData(stationId, startTime, endTime, tenantOperatorCode);
    }

    @Override
    public int countByNotReviewedOrder(String tenantCode) {
        return acctBillOrderMapper.countByNotReviewedOrder(tenantCode);
    }

    @Override
    public List<AcctBillOrderResultVo> listAcctBillOrderForComplete(String tenantOperatorCode) {
        return acctBillOrderMapper.listAcctBillOrderForComplete(tenantOperatorCode);
    }

    @Override
    public List<AcctBillOrderResultVo> exportAcctBillOrder(AcctBillOrderSearch acctBillOrderSearch) {
        return acctBillOrderMapper.listPageAcctBillOrder(acctBillOrderSearch);
    }

    @Override
    public List<OrderPrintVo> exportOrderInfo(OrderAcctBillOrderSearch orderSearch) {
        return acctBillOrderMapper.exportOrderInfo(orderSearch);
    }

    @Override
    public void updateFactAmount(Long billOrderId) {
        acctBillOrderMapper.updateFactAmount(billOrderId);
    }

    @Override
    public void updateForStatus(AcctBillOrder acctBillOrder) {
        if ("3".equals(acctBillOrder.getStatus())) {
            AcctBillOrder billOrder = acctBillOrderMapper.selectByPrimaryKey(acctBillOrder.getBillOrderId());

            TenantOperatorAccountRecord tenantOperatorAccountRecord = new TenantOperatorAccountRecord();
            tenantOperatorAccountRecord.setTenantCode(billOrder.getTenantCode());
            tenantOperatorAccountRecord.setTenantOperatorCode(billOrder.getTenantOperatorCode());
            tenantOperatorAccountRecord.setBillOrderId(billOrder.getBillOrderId());
            tenantOperatorAccountRecord.setAccountType("1");
            tenantOperatorAccountRecord.setRecordType("0");
            tenantOperatorAccountRecord.setAmount(matchNull(billOrder.getFactAmount()));

            TenantOperatorAccount tenantOperatorAccount = tenantOperatorAccountService.selectByPrimaryKey(billOrder.getTenantOperatorCode());
            if (tenantOperatorAccount==null){
                TenantOperatorAccount tenantOperatorAccount1=new TenantOperatorAccount();
                tenantOperatorAccount1.setTenantCode(billOrder.getTenantCode());
                tenantOperatorAccount1.setTenantOperatorCode(billOrder.getTenantOperatorCode());
                tenantOperatorAccount1.setCreatedTime(new Date());

                tenantOperatorAccountService.insertSelective(tenantOperatorAccount1);

                tenantOperatorAccountRecord.setResult(matchNull(tenantOperatorAccount1.getAccount()).add(matchNull(billOrder.getFactAmount())));
                tenantOperatorAccountRecordService.insertSelective(tenantOperatorAccountRecord);

                tenantOperatorAccount1.setBillAccount(matchNull(tenantOperatorAccount1.getBillAccount()).add(matchNull(billOrder.getFactAmount())));
                tenantOperatorAccount1.setAccount(matchNull(tenantOperatorAccount1.getAccount()).add(matchNull(billOrder.getFactAmount())));
                tenantOperatorAccountService.updateByPrimaryKeySelective(tenantOperatorAccount1);
            }else {
                tenantOperatorAccountRecord.setResult(matchNull(tenantOperatorAccount.getAccount()).add(matchNull(billOrder.getFactAmount())));
                tenantOperatorAccountRecordService.insertSelective(tenantOperatorAccountRecord);

                tenantOperatorAccount.setBillAccount(matchNull(tenantOperatorAccount.getBillAccount()).add(matchNull(billOrder.getFactAmount())));
                tenantOperatorAccount.setAccount(matchNull(tenantOperatorAccount.getAccount()).add(matchNull(billOrder.getFactAmount())));
                tenantOperatorAccountService.updateByPrimaryKeySelective(tenantOperatorAccount);
            }

        }
        acctBillOrderMapper.updateByPrimaryKeySelective(acctBillOrder);
    }

    @Override
    public int countByGeneratedOrder(String tenantCode) {
        return acctBillOrderMapper.countByGeneratedOrder(tenantCode);
    }

    @Override
    public void saveAcctBillOrder(AcctBillOrder acctBillOrder) {
        Long lastId = this.selectLastIdByDay(acctBillOrder.getTenantCode());
        //创建结算单号
        acctBillOrder.setBillOrderNum(uidGenerator.genBillOrderNum("",lastId));
        acctBillOrder.setRunTime(new Date());
        this.insertSelective(acctBillOrder);
    }

    @Override
    public Long selectLastIdByDay(String tenantCode) {
        return acctBillOrderMapper.selectLastIdByDay(tenantCode);
    }

    @Override
    public AcctBillOrderResultVo selectSupplyAcctBillOrder(Long billOrderId) {
        return acctBillOrderMapper.selectSupplyAcctBillOrder(billOrderId);
    }

    public BigDecimal matchNull(BigDecimal o) {
        return o == null ? BigDecimal.ZERO : o;

    }

}
